﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

namespace tmAsk.Common
{
    public class alldata
    {
        public static string Config_Path = (new alldata()).ApplicationPath();
        public static string Config_ischeck = "0";
        public static string Config_point = "0";
        public static string Config_time = "0";
        public static string Config_name = "";
        public static string Config_reg = "0";
        public static string Config_template = "";
        public static string Config_defaultname = "";
        public static string Config_askupload = "0";
        public static string Config_reupload = "0";
        public static string Config_defaultimg = "/images/face/default.jpg";
        public string Config_url = "";

        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();
        //系统初始化变量
        public alldata()
        {
            DataTable C = Gatatable("select c_isCheck,c_defaultpoint,c_setTime,c_title,c_isVisitors,c_template,c_defaultname,c_defaultimg,c_index,c_askupload,c_reupload from B_config");
            if (C.Rows.Count > 0)
            {
                Config_ischeck = C.Rows[0]["c_isCheck"].ToString();
                Config_point = C.Rows[0]["c_defaultpoint"].ToString();
                Config_time = C.Rows[0]["c_setTime"].ToString();
                Config_name = C.Rows[0]["c_title"].ToString();
                Config_reg = C.Rows[0]["c_isVisitors"].ToString();
                Config_template = C.Rows[0]["c_template"].ToString();
                Config_defaultname = C.Rows[0]["c_defaultname"].ToString();
                Config_url = C.Rows[0]["c_index"].ToString();
                Config_askupload = C.Rows[0]["c_askupload"].ToString();
                Config_reupload = C.Rows[0]["c_reupload"].ToString();
                Config_defaultimg = C.Rows[0]["c_defaultimg"].ToString();
            }
        }
        
        //获得应有路径
        public string ApplicationPath()
        {
            string applicationPath = HttpContext.Current.Request.ApplicationPath;

            if (applicationPath == "/")
            {
                return string.Empty;
            }
            else
            {
                return applicationPath;
            }        
        }

        //根据分类ID获取分类名称
        public string GetClassName(string id)
        {
            int classid = Convert.ToInt16(id);
            DataTable rs = Gatatable("select a_className from B_askClass where a_classid = " + classid);
            if (rs.Rows.Count > 0)
            {
                return rs.Rows[0]["a_className"].ToString();
            }
            return "";
        }

        //获取赞成比例
        public string GetRatio(string str)
        {
            string sqlnum = "select r_agreeNumber,a_id,r_unagreeNumber from b_review where r_id=" + str;
            DataTable dr = Gatatable(sqlnum);
            double count = 0;
            if (dr.Rows.Count > 0)
            {
                if ((int)dr.Rows[0]["r_agreeNumber"] == 0 && (int)dr.Rows[0]["r_unagreeNumber"] == 0)
                {
                    return "0%";
                }
                if ((int)dr.Rows[0]["r_unagreeNumber"] != 0)
                {
                    count = Math.Round(Convert.ToDouble((int)dr.Rows[0]["r_agreeNumber"]) / ((int)dr.Rows[0]["r_unagreeNumber"] + (int)dr.Rows[0]["r_agreeNumber"]), 2);
                }
                else
                {
                    return "100%";
                }
            }
            return ((count * 100) + "%").ToString();
        }
        //获取相关问题内容
        public string LikeQuestion(string parm)
        {
            string str = parm.Split('^')[0];
            string Id = parm.Split('^')[1];
            if (str.Trim().Length == 0)
            {
                return "";
            }
            string html = string.Empty;
            string[] arr = str.Split(new char[] { ',', '，' });
            string sql = "select TOP 5 a_title,a_id,a_state,a_time from b_ask where a_id <> " + Id;
            for (int i = 0; i < arr.Length; i++)
            {
                sql += " and a_keyword like '%" + arr[i] + "'";
            }
            DataTable dt = Gatatable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                html += "<div class=\"wenti\"><div class=\"wenti1\">";
                if (dt.Rows[i]["a_state"].ToString() == "1")
                {
                    html += "<img src=\"images/duihao.gif\" width=\"14\" height=\"14\" />&nbsp;";
                }
                else
                {
                    html += "<img src=\"images/wenhao22.gif\" width=\"14\" height=\"14\" />&nbsp;";
                }
                string temp = function.CutString(dt.Rows[i]["a_title"].ToString(), "30");
                for (int n = 0; n < arr.Length; n++)
                {
                    temp = "<a href='A_" + (new function()).Md5(dt.Rows[i]["a_id"].ToString()) + ".html'>" + temp.Replace(arr[n], "<span style=\"color:red;\">" + arr[n] + "</span>") + "</a>";
                }
                html += temp;

                html += "</div><div class=\"wenti2\">" + ((DateTime)dt.Rows[i]["a_time"]).ToString("d") + "</div></div>";
            }
            return html;
        }

        //判断是否选择
        public string GetSelected(string classid, string id)
        {
            DataTable dt = Gatatable("select a_parentid from B_askClass where a_classid=" + id);
            if (dt.Rows.Count > 0)
            {
                if (classid == dt.Rows[0]["a_parentid"].ToString())
                {
                    return "selected='selected'";
                }
            }
            return "";
        }

        //模板路径解析
        public string Gettemplate(int i)
        {
            DataTable rs = Gatatable("select m_template from B_makehtml where m_url = '" + i + "'");
            if (rs.Rows.Count > 0)
            {
                return rs.Rows[0]["m_template"].ToString();
            }
            return "";
        }

        //系统table
        private DataTable Gatatable(string sql)
        {
            return SQL.GetList(sql).Tables[0];
        }


        /// <summary>
        /// email通知
        /// </summary>
        /// <param name="etype">类型</param>
        /// <param name="userid">接收人ID</param>
        /// <param name="title">发布内容</param>
        public void sendemail(string etype, string userid, string title)
        {
            //回复内容，给作者发email
            string emailsql = "insert into B_msg (m_type,m_acceptuser,m_content,m_time,m_isread)values(";
            emailsql += "@m_type,@m_acceptuser,@m_content,@m_time,@m_isread)";

            SqlParameter[] parameters = new SqlParameter[]{
            new SqlParameter("@m_type",etype),
            new SqlParameter("@m_acceptuser",userid),
            new SqlParameter("@m_content",title),
            new SqlParameter("@m_time",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")),
            new SqlParameter("@m_isread","0")
        };
            SQL.ExecuteSql(emailsql, parameters);
        }

        public string username(int userid)
        {
            DataTable rs = Gatatable("select u_email from B_user where u_id = " + userid);
            if (rs.Rows.Count > 0)
            {
                return rs.Rows[0]["u_email"].ToString();
            }
            return "";
        }

        public void update_search(string key, string askid)
        {
            string sql = "select s_askid from B_searchTitel where s_askid=@s_askid";
            SqlParameter[] param = new SqlParameter[]
        {
            new SqlParameter("@s_askid",askid)
        };
            if (SQL.GetList(sql, param).Tables[0].Rows.Count <= 0)
            {
                string sqlask = "select u_id,a_title,a_classid from B_ask where a_id=@askid";
                SqlParameter[] paramask = new SqlParameter[]
            {
                 new SqlParameter("@askid",askid)
            };
                DataTable rs = SQL.GetList(sqlask, paramask).Tables[0];
                SQL.ExecuteSql("insert into B_searchTitel (s_title,s_key,s_userid,s_time,s_classid,s_askid)values('" + rs.Rows[0]["a_title"].ToString() + "','" + key + "'," + Convert.ToInt16(rs.Rows[0]["u_id"]) + ",'" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "'," + Convert.ToInt16(rs.Rows[0]["a_classid"]) + "," + Convert.ToInt16(askid) + ")");
            }
        }
        public string usersorce(string username)
        {
            DataTable rs = Gatatable("select u_point from B_user where u_email = '" + username + "'");
            if (rs.Rows.Count > 0)
            {
                return rs.Rows[0]["u_point"].ToString();
            }
            return "";
        }
    }
}
